﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WXshopBuiness;
using Services.WXshopBuiness;

namespace WeiXinProject.Controllers
{
    public class AddressController : WXBaseController
    {
        AddressServices m_AddressServices;
        public AddressController(AddressServices mAddressServices)
        {
            m_AddressServices = mAddressServices;
        }
        // GET: Address
        public ActionResult Index()
        {
            return View();
        }

        public JsonResult GetAddressById(string addressID)
        {
            var jsonData = new Object();
            string result = string.Empty;
            Address ad = m_AddressServices.GetById(addressID);
            jsonData = new { Success = true, Data = ad };
            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 我的收货地址
        /// </summary>
        /// <returns></returns>
        public ActionResult MyAddress()
        {
            string userId = this.CurrentUser.UserID;
            List<Address> address = m_AddressServices.GetMyAddress(userId);
            return View(address);
        }

        public ActionResult AddressEdit(string addressId)
        {
            Address ad = m_AddressServices.GetById(addressId);
            return View(ad);
        }
        [HttpPost]
        public JsonResult EditAddr()
        {
            var jsonData = new Object();
            string result = string.Empty;            
            bool success = false;
            string Id = Request.Form["addressId"];
            Address ad = new Address();
            if (!string.IsNullOrEmpty(Id))
            {
                ad = m_AddressServices.GetById(Id);
            }
            else
            {              
                ad.AddressID = Guid.NewGuid().ToString();
            }
            ad.Sheng = Request.Form["sheng"];
            ad.Shi = Request.Form["shi"];
            ad.QuYu = Request.Form["quyu"];
            ad.Area =ad.Sheng+ad.Shi+ad.QuYu;
            ad.DetailAddress = Request.Form["DetailAddress"];
            string defaultstr = Request.Form["IsDefault"];
            ad.IsDefault =int.Parse(!string.IsNullOrEmpty(defaultstr)? (defaultstr=="true"?"1":"0"): "0");
            ad.Phone = Request.Form["Phone"];
            ad.PostCode = Request.Form["PostCode"];
            ad.Receiver = Request.Form["Receiver"];
            string sorindexstr = Request.Form["SortIndex"];
            ad.SortIndex =int.Parse(!string.IsNullOrEmpty(sorindexstr)?sorindexstr:"0" );
            ad.Tag = Request.Form["Tag"];
            ad.UserId = this.CurrentUser.UserID;
            if (!string.IsNullOrEmpty(Id))
            {
                success = m_AddressServices.EditAddress(ad);
            }
            else {
                success = m_AddressServices.AddAddress(ad);
            }

            result = success ? "编辑成功" : "编辑失败";
            jsonData = new { Success = success, Data = result, AddressId=ad.AddressID };

            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 删除地址
        /// </summary>
        /// <param name="addressId"></param>
        /// <returns></returns>
        public JsonResult DelAddr(string addressId)
        {
            var jsonData = new Object();
            string result = string.Empty;
            bool success = false;
            success = m_AddressServices.DelAddress(addressId);
            result = success ? "删除成功" : "删除失败";
            jsonData = new { Success = success, Data = result};

            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }
    }
}